//
//  ColorViewController.m
//  Blue-toothLight
//
//  Created by mazg on 15/6/20.
//  Copyright (c) 2015年 mazg. All rights reserved.
//

#import "ColorViewController.h"
#import "LightTabViewController.h"
#import "LightModel.h"
#import "NSManagedObject+helper.h"

#import "UIImage+ColorAtPixel.h"
#import "UIImage+Tool.h"


#define WinWidth [UIScreen mainScreen].bounds.size.width
#define WinHeight [UIScreen mainScreen].bounds.size.height

#define W(x) WinWidth*x/320.0
#define H(y) WinHeight*y/568.0

@interface ColorViewController (){
    UIImageView *selImgView;
    //下面申明三个变量是为了避免在很短的时间内频繁的向设备发送数据
    int lastSliderValue ;
    int moveCount;
    int lastMoveCount;
    
}

@end

@implementation ColorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1、初始化imgView
    UIImage *img=[UIImage imageWithName:@"pickerColorWheel133@2x.png" border:5.0f borderColor:[UIColor lightGrayColor]];
    self.colorView=[[UIImageView alloc]initWithImage:img];
    self.colorView.center=CGPointMake(WinWidth/2.0f, WinHeight/2.0f-20);
    [self.view addSubview:self.colorView];
    
    
    //设置默认选中颜色
    LightTabViewController *lightTab=(LightTabViewController *)self.tabBarController;
    for (LightModel *model in lightTab.currentLightList) {
        
        self.selColor=model.perColor;
        CGFloat r,g,b,a;
        [self.selColor getRed:&r green:&g blue:&b alpha:&a];
        _colorSlider.value=a*255.0f;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



//触摸开始
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch* touch = [touches anyObject];
    BOOL canMoved =[self getNewColorWithTouch:touch];
    if (canMoved==NO) {
        return;
    }

    NSArray *tmpArr=[self getCurrentPeripheralArr];
    for (CBPeripheral *cbp in tmpArr) {
        [self.colorInstance changeColorWithPeripheral:cbp ToTarget:self.selColor];
    }
}



//从图片中获取颜色
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    BOOL canMoved =[self getNewColorWithTouch:touch];
    if (canMoved==NO) {
        return;
    }
    moveCount++;
    if (moveCount-lastMoveCount>10) {
        NSArray *tmpArr=[self getCurrentPeripheralArr];
        for (CBPeripheral *cbp in tmpArr) {
            [self.colorInstance changeColorWithPeripheral:cbp ToTarget:self.selColor];
        }
        lastMoveCount=moveCount;
    }
}



//滑块变动后
- (IBAction)sliderChanged:(id)sender {
    UISlider *slider=sender;
    CGFloat r,g,b,a;
    [self.selColor getRed:&r green:&g blue:&b alpha:&a];
    r=r*255.0f;
    g=g*255.0f;
    b=b*255.0f;
    a=a*255.0f;
    
//    NSLog(@"%f,%f,%f,%f",r,g,b,a);
    
    a=slider.value;
    
    self.selColor=[UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a/255.0f];
    UIImage *newImg=[UIImage imageWithName:@"pickerColorWheel133@2x.png" border:5.0f borderColor:[UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:(1-a/255.0f)]];
    _colorView.image=newImg;
    
    if (a-lastSliderValue>10||a-lastSliderValue<-10) {
        NSArray *tmpArr=[self getCurrentPeripheralArr];
        for (CBPeripheral *cbp in tmpArr) {
            [self.colorInstance changeColorWithPeripheral:cbp ToTarget:self.selColor];
        }
        lastSliderValue=a;
    }
}


-(BOOL )getNewColorWithTouch:(UITouch *)touch{
    //1、判断触摸点是否在圆形区域内
    CGPoint viewPoint=[touch locationInView:self.view];
    CGFloat raidus=self.colorView.frame.size.width/2.0f-5;
    if (![self pointInCircleRect:viewPoint radius:raidus]) {
        return NO;
    }
    _colorSlider.enabled=YES;
    //2、获取手指触摸点的颜色信息，这里－5是因为colorView中的真正图片外面包了5个像素点的颜色值
    CGPoint point = [touch locationInView:_colorView];
    point.x-=5;
    point.y-=5;
    UIImage *img=[UIImage imageNamed:@"pickerColorWheel133@2x.png"];
    self.selColor=[img colorAtPixel:point];
    
    CGFloat r,g,b,a;
    [self.selColor getRed:&r green:&g blue:&b alpha:&a];
    
    //    NSLog(@"选中的颜色是:R:%f,G:%f,B:%f,alpha:%f",r*255.0f,g*255.0f,b*255.0f,a*255.0f);
    
    if (r==0 && g==0 &&b==0) {
        return NO;
    }
    
    //3、改变所获取的颜色值的aplpa，得到一个新的颜色,更新imgView的边框圆颜色，同时向蓝牙设备发送这个新颜色
    r=r*255.0f;
    g=g*255.0f;
    b=b*255.0f;
    a=_colorSlider.value;
    
    self.selColor=[UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a/255.0f];
    UIImage *newImg=[UIImage imageWithName:@"pickerColorWheel133@2x.png" border:5.0f borderColor:[UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:(1-a/255.0f)]];
    _colorView.image=newImg;
    return YES;
}

//判断触摸点是否在圆形区域内
-(BOOL)pointInCircleRect:(CGPoint)point radius:(CGFloat)r{
    CGPoint center=self.colorView.center;
    int spaceX=point.x-center.x;
    int spaceY=point.y-center.y;
    CGFloat f=sqrt(spaceX*spaceX+spaceY*spaceY);
    if (f<r) {
        return YES;
    }
    else{
        return NO;
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    LightTabViewController *lightTab=(LightTabViewController *)self.tabBarController;
    for (LightModel *model in lightTab.currentLightList) {
        model.perColor=self.selColor;

//        CGFloat r,g,b,a;
//        [self.selColor getRed:&r green:&g blue:&b alpha:&a];
//        NSLog(@"保存颜色值:%f,%f,%f,%f",r*255.0f,g*255.0f,b*255.0f,a*255.0f);
        
        [LightModel save:^(NSError *error) {
            
        }];
    }
    self.selColor=nil;
}



@end
